﻿using CefSharp;
using CefSharp.DevTools.FedCm;
using KeZhou.Models.Instances;
using KeZhou.Views.Components.Controls;
using Models.Wishire;
using Models.Wishire.entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace KeZhou.ViewModels
{
    internal class TargetManageViewModel
    {
        private static TargetManageViewModel instance;
        public static TargetManageViewModel Instance
        {
            get
            {
                return instance ?? (instance = new TargetManageViewModel());
            }
        }
        solve solve = new solve();
        //初始化
        TargetManageViewModel()
        {
            
        }

        //查-获取plan

        public void getAllPlanByAccountId()
        {
            //TODO@wxy
            //初始化时获取当前用户所有plan
            int accountId = (int)PlanOnBuilding.Instance.accountId;

            var ans = solve.find_plan(accountId, PlanOnBuilding.Instance.cityName);
            List<PageFourPlan> allPlanList = new List<PageFourPlan>();
            for (int i = 0; i < ans.Count; i++)
            {
                allPlanList.Add(new PageFourPlan(
                    ans[i].GetPlanId(),
                    ans[i].GetPlanName(),
                    ans[i].GetCityName(),
                    ans[i].GetStartTime(),
                    ans[i].GetEndTime(),
                    ans[i].GethotelName(),
                    ans[i].GethotelLat(),
                    ans[i].GethotelLng(),
                    ans[i].GetEmpty())
                    );
            }
            //end
            var json = JsonConvert.SerializeObject(new { planList = allPlanList, cityName = PlanOnBuilding.Instance.cityName });
            MainFourWishToGo.Instance.Browser.ExecuteScriptAsyncWhenPageLoaded($"document.getAllPlans({json});");
        }

        //查-通过planId获取所有target
        public void getAllTargetsByPlanId(int planId)
        {
            //TODO@wxy
            //根据planId获取planId下的所有target
            var ans = solve.find_all_target(planId);

            List<PageFourTarget> allTargets = new List<PageFourTarget>();
            for (int i = 0; i < ans.Count; i++)
            {
                allTargets.Add(new PageFourTarget(
                    ans[i].GetTargetId(),
                    ans[i].GetTargetCity(),
                    ans[i].GetTargetAddress(),
                    ans[i].GetTargetName(),
                    ans[i].GetTargetType(),
                    ans[i].GetMustNight(),
                    ans[i].GetEmpty())
                    );
            }
            {
            };
            //end
            var json = JsonConvert.SerializeObject(new { allTargets = allTargets });
            Debug.WriteLine(json);
            MainFourWishToGo.Instance.Browser.ExecuteScriptAsyncWhenPageLoaded($"document.getAllTargetsByPlanId({json});");
        }

        //删-删除一个plan
        public void deletePlanByPlanId(int planId)
        {
            //TODO@wxy
            //根据planId删除plan
            solve.delete_byplanId(planId);
            //end
            getAllPlanByAccountId();
        }

        //删-删除一个target
        public void deleteTargetByTargetId(int targetId)
        {
            //TODO@wxy
            solve.delete_target(targetId);
            //end
            //前端删除，后续无需再获取target
        }

        //改-更改一个plan
        public void updatePlan(PageFourPlan plan)
        {
            //TODO@wxy
            //更新传来的plan
            //solve.find
            int accountId = solve.findplan_byId(plan.planId).GetAccountId();
            plan_st newplan = new plan_st();
            newplan.SetPlanId(plan.planId);
            newplan.SetAccountId(accountId);
            newplan.SetPlanName(plan.planName);
            newplan.SetCityName(plan.cityName);
            newplan.SetStartTime(plan.startTime);
            newplan.SetEndTime(plan.endTime);
            newplan.SethotelName(plan.hotelName);
            newplan.SethotelLat(plan.hotelLat);
            newplan.SethotelLng(plan.hotelLng);
            newplan.SetEmpty(plan.empty);
            solve.update_plan(newplan);
            //end
        }

        //改-更改一个target
        public void updateTarget(PageFourTarget target)
        {
            //TODO@wxy
            //更新传来的target
            target_st newtarget = new target_st();
            newtarget.SetTargetId(target.targetId);
            newtarget.SetTargetCity(target.targetCity);
            newtarget.SetTargetAddress(target.targetAddress);
            newtarget.SetTargetName(target.targetName);
            newtarget.SetTargetType(target.targetType);
            newtarget.SetMustNight(target.mustNight);
            newtarget.SetEmpty(target.empty);
            solve.update_target(newtarget);
            //end
        }

        //跳转-当前计划更新旅行方案
        public void jumpToFinalPlan(int planId)
        {

            //TODO@wxy
            //数据库里看这个planId对应的plan有没有旅行方案answer，
            //没有的话生成旅行方案
            //有的话清空answer，重新生成旅行方案
            solve.generate(planId);
            //这里是传来的planId
            //根据planId查到plan
            var plan = solve.findplan_byId(planId);
            PageFourPlan currentPlan = new PageFourPlan(
                    plan.GetPlanId(),
                    plan.GetPlanName(),
                    plan.GetCityName(),
                    plan.GetStartTime(),
                    plan.GetEndTime(),
                    plan.GethotelName(),
                    plan.GethotelLat(),
                    plan.GethotelLng(),
                    plan.GetEmpty()
                    );
            //获取到生成的answer的list
            var ans = solve.generate(planId);
            List<PageFiveAnswer> currentPlanAnswer = new List<PageFiveAnswer>();
            for(int i=0;i<ans.Count;i++)
            {
                currentPlanAnswer
                    .Add(new PageFiveAnswer(
                        ans[i].GetAnswerId(),
                        ans[i].GetPlanId(), ans[i].GetName(), ans[i].GetIfDeleted(),
                        ans[i].GetIndex(),
                        ans[i].GetLat(),
                        ans[i].GetLng()));
            };
            //end
            Application.Current.Dispatcher.Invoke(() => 
            {
                PlanViewModel.Instance.jumpFromInitPlan(currentPlan, currentPlanAnswer);
                MainSkeletonViewModel.Instance.changeMode(4);
            });
            
        }
    }

    //中转，plan类
    class PageFourPlan
    {
        public int planId { get; set; }
        public string planName { get; set; }
        public string cityName { get; set; }
        public string startTime { get; set; }
        public string endTime { get; set; }
        public string hotelName { get; set; }
        public double hotelLat { get; set; }
        public double hotelLng { get; set; }
        public string empty { get; set; }//计划备注

        public PageFourPlan() { }

        public PageFourPlan(int planId, string planName, string cityName, string startTime, string endTime,
                        string hotelName, double hotelLat, double hotelLng, string empty)
        {
            this.planId = planId;
            this.planName = planName;
            this.cityName = cityName;
            this.startTime = startTime;
            this.endTime = endTime;
            this.hotelName = hotelName;
            this.hotelLat = hotelLat;
            this.hotelLng = hotelLng;
            this.empty = empty;
        }

    }

    //中转，target类
    class PageFourTarget
    {
        public int targetId { get; set; }
        public string targetCity { get; set; }
        public string targetAddress { get; set; }
        public string targetName { get; set; }
        public string targetType { get; set; }
        public int mustNight { get; set; }
        public string empty { get; set; }

        public PageFourTarget() { }

        public PageFourTarget(int targetId, string targetCity, string targetAddress, string targetName, string targetType, int mustNight, string empty)
        {
            this.targetId = targetId;
            this.targetCity = targetCity;
            this.targetAddress = targetAddress;
            this.targetName = targetName;
            this.targetType = targetType;
            this.mustNight = mustNight;
            this.empty = empty;
        }
    }

    //中转，answer类
    class PageFiveAnswer
    {
        public int answerId { get; set; }
        public int planId { get; set; }
        public string name { get; set; }
        public int ifDeleted { get; set; }
        public int answerIndex { get; set; }
        public double lat { get; set; }
        public double lng { get; set; }

        public PageFiveAnswer() { }

        public PageFiveAnswer(int answerId, int planId, string name, int ifDeleted, int answerIndex, double lat, double lng)
        {
            this.answerId = answerId;
            this.planId = planId;
            this.name = name;
            this.ifDeleted = ifDeleted;
            this.answerIndex = answerIndex;
            this.lat = lat;
            this.lng = lng;
        }

    }

}
